// pages/search/search.js

const app = getApp()
Page({
  data: {
    history: [],
    isShow: false,
    isCollapse: true,
    height: '',
    statusBarHeight: app.globalData.statusBarHeigh,
    value: ''
  },

  search(e) {
    let value = this.data.value
    if (value == '') return
    let his = wx.getStorageSync('searchHistory')
    if (his == '') his = []
    for (let x in his) {
      if (his[x] == value) {
        this.navResult(value)
        return
      }
    }
    his.unshift(value)
    wx.setStorageSync('searchHistory', his)
    this.setData({
      history: his
    })
    this.navResult(value)
    this.isShow()
  },

  clickItem(e) {
    let key = e.currentTarget.dataset.item
    this.navResult(key)
  },

  navResult(key) {
    wx.$nav('/pages/searchResult/searchResult', {
      searchKey: key
    })
  },

  onShow: function () {
    let his = wx.getStorageSync('searchHistory')
    this.setData({
      history: his
    })
    this.isShow()
  },
  //判断全文收起是否展示
  isShow: function () {
    var query = wx.createSelectorQuery();
    var that = this;
    query.selectAll('.hisCon').boundingClientRect(function (rect) {
      rect.forEach((v, i) => {
        that.setData({
          isShow: v.height > 60 ? true : false,
          height: v.height
        })
      })
    }).exec();
  },
  //点击收起
  changeCollapse: function () {
    this.setData({
      isCollapse: !this.data.isCollapse
    })
  },
  return () {
    wx.navigateBack()
  },
  inp(e) {
    this.setData({
      value: e.detail.value
    })
  },
  onLoad() {
    this.setData({
      statusBarHeight: app.globalData.statusBarHeight
    })
  }
})